import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_bloc.dart';
import 'package:flutter_learn_vi/src/bean/bean_mine.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/model/bloc_model.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/page/mine/mine_address_mine_depart_page.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/17.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class MineBlocAddressItemPage extends StatefulWidget {
  @override
  _MineBlocAddressItemPageState createState() =>
      _MineBlocAddressItemPageState();
}

class _MineBlocAddressItemPageState extends State<MineBlocAddressItemPage>
    with BlocModel {
  @override
  void initState() {
    super.initState();

    if (UserHelper.getInstance.userBean.userBlocType == 1) {
      getBlocDetails();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: buildGridView(),
      appBar: AppBar(
        title: Text("集团通讯录"),
      ),
    );
  }

  Widget buildGridView() {
    if (_blocBean == null ||
        _blocBean.mineEntityList == null ||
        _blocBean.mineEntityList.length == 0) {
      return NoDataWidget(
        clickCallBack: () {
          getBlocDetails();
        },
      );
    }
    return ListView.builder(
      itemCount: _blocBean.mineEntityList.length,
      itemBuilder: (BuildContext context, int index) {
        return buildListViewItemWidget(index);
      },
    );
  }

  Widget buildListViewItemWidget(int index) {
    MineBean mineBean = _blocBean.mineEntityList[index];
    return InkWell(
      onTap: () {
        NavigatorUtils.pushPage(
            context, MineSelectDepartmentPage(mineBean: mineBean));
      },
      child: Container(
        margin: EdgeInsets.only(left: 14, right: 14, top: 10),
        padding: EdgeInsets.only(left: 20, top: 20, bottom: 20, right: 12),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(3))),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            buildImage(mineBean),
            SizedBox(
              width: 18,
            ),
            Expanded(
              child: Text(
                "${mineBean.name}",
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
              ),
            ),
            Icon(Icons.arrow_right)
          ],
        ),
      ),
    );
  }

  Widget buildImage(MineBean mineBean) {
    String url = mineBean.logoUrl;
    if (url == null || url.length == 0) {
      return ClipOval(
        child: Image.asset(
          "assets/images/3.0x/bg_header.png",
          width: 44,
          height: 44,
          fit: BoxFit.fill,
        ),
      );
    }
    return CachedNetworkImage(
      imageUrl: mineBean.logoUrl,
      width: 44,
      height: 44,
    );
  }

  BlocBean _blocBean;
  bool isLoading = true;

  Future<bool> getBlocDetails() async {
    //获取集团信息
    _blocBean = await getBlocDetailsRquest();
    isLoading = false;
    if (!mounted) return true;
    setState(() {});
    return Future.value(true);
  }
}
